package com.web.portal.service;

import java.util.EnumMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.web.portal.dao.BaseDao;
import com.web.portal.dao.FacultySearchType;
import com.web.portal.dao.WebportalSearchType;
import com.web.portal.entity.Admin;
import com.web.portal.entity.Faculty;

@Service(value = "facultyService")
public class DefaultFacultyService extends DefaultWebPortalService<Faculty>
		implements FacultyService {
	private final static Logger LOG = Logger
			.getLogger(DefaultFacultyService.class);

	@Autowired
	@Qualifier(value = "facultyDao")
	public void init(BaseDao dao) {
		setBaseDao(dao);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.web.portal.service.FacultyService#addFaculty(com.web.portal.entity
	 * .Faculty) add faculty
	 */
	@Transactional
	public void addFaculty(Faculty faculty) {
		createOrUpdate(faculty);

	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	protected boolean isCreate(Faculty entity) throws RuntimeException {
		// TODO Auto-generated method stub
		return false;
	}

	public List<Faculty> listAllFaculty(Faculty faculty) {
		return getList(faculty);

	}

	public boolean isAddedAlready(String email) {
		final EnumMap<FacultySearchType, Object> searchCriteria = new EnumMap(
				FacultySearchType.class);

		searchCriteria.put(FacultySearchType.EMAIL, email);
		List<Faculty> result = searchByCriteria(searchCriteria);
		if (!result.isEmpty() && result.size() == 1) {
			LOG.debug("user is already Added!");
			return true;
		}
		LOG.debug("unique user added");
		return false;

	}

	public Faculty getFacultyImageOrMapFile(String email) {
		// TODO Auto-generated method stub
		final EnumMap<FacultySearchType, Object> searchCriteria = new EnumMap(
				FacultySearchType.class);

		searchCriteria.put(FacultySearchType.EMAIL, email);
		List<Faculty> result = searchByCriteria(searchCriteria);
		if (!result.isEmpty() && result.size() == 1) {
			LOG.debug("Faculty map or Image found..............!");
			return result.get(0);
		}
		LOG.debug("Faculty Image or map not found.................");
		return null;

	}

	public void deleteFaculty(Faculty faculty) {
		// TODO Auto-generated method stub
		deleteFaculty(faculty);
	}

	public Faculty getFaculty(String id) {
		// TODO Auto-generated method stub
		final EnumMap<FacultySearchType, Object> searchCriteria = new EnumMap(
				FacultySearchType.class);

		searchCriteria.put(FacultySearchType.ID, id);
		List<Faculty> result = searchByCriteria(searchCriteria);
		if (!result.isEmpty() && result.size() == 1) {
			LOG.debug("Faculty found ..............!");
			return result.get(0);
		}
		LOG.debug("Faculty found.................");
		return null;
	}
}
